<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/reimupage/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/reimupage/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/reimupage/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/reimupage/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/reimupage/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/reimupage/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/reimupage/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta property="og:type" content="website">
<meta property="og:title" content="Reimu&#39;Blog">
<meta property="og:url" content="http://hatsunem1ku.gitee.io/reimupage/index.html">
<meta property="og:site_name" content="Reimu&#39;Blog">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Reimu&#39;Blog">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/reimupage/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://hatsunem1ku.gitee.io/reimupage/"/>





  <title>Reimu'Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/reimupage/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Reimu'Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">求知若饥 虚心若愚</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/reimupage/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/reimupage/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/reimupage/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/reimupage/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/reimupage/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://hatsunem1ku.gitee.io/reimupage/reimupage/2018/05/21/vue-cli项目代码结构介绍/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Reimu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/reimupage/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Reimu'Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/reimupage/2018/05/21/vue-cli项目代码结构介绍/" itemprop="url">vue-cli 项目代码结构介绍</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-21T21:45:21+08:00">
                2018-05-21
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/reimupage/categories/vue/" itemprop="url" rel="index">
                    <span itemprop="name">vue</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="vue-cli-项目代码结构介绍"><a href="#vue-cli-项目代码结构介绍" class="headerlink" title="vue-cli 项目代码结构介绍"></a>vue-cli 项目代码结构介绍</h2><hr>
<table>
<thead>
<tr>
<th>文件名</th>
<th>介绍</th>
</tr>
</thead>
<tbody>
<tr>
<td>README.md</td>
<td>项目说明文件</td>
</tr>
<tr>
<td>package-lock.json</td>
<td>第三方包的具体版本</td>
</tr>
<tr>
<td>package.json</td>
<td>项目的配置文件，存放项目中依赖的库</td>
</tr>
<tr>
<td>LICENSE</td>
<td>开源协议的说明</td>
</tr>
<tr>
<td>index.html</td>
<td>项目默认的首页模板文件</td>
</tr>
<tr>
<td>.project</td>
<td></td>
</tr>
<tr>
<td>.postcssrc.js</td>
<td>postcss的配置项</td>
</tr>
<tr>
<td>.gitignore</td>
<td>一些不需要传到git仓库中的文件</td>
</tr>
<tr>
<td>.eslintrc.js</td>
<td>配置代码的规范</td>
</tr>
<tr>
<td>.eslintignore</td>
<td>不受eslint监测的文件</td>
</tr>
<tr>
<td>.editorconfig</td>
<td>配置编辑器中的语法</td>
</tr>
<tr>
<td>.babelrc</td>
<td>babel语法解析器的配置文件</td>
</tr>
<tr>
<td>static文件夹</td>
<td>存放静态资源</td>
</tr>
<tr>
<td>node_modules</td>
<td>项目依赖的第三方node包</td>
</tr>
<tr>
<td>src文件夹</td>
<td>整个项目的源代码</td>
</tr>
<tr>
<td>build文件夹</td>
<td>webpack的配置项</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>src文件夹中的文件</th>
<th>介绍</th>
</tr>
</thead>
<tbody>
<tr>
<td>main.js</td>
<td>整个项目的入口文件</td>
</tr>
<tr>
<td>App.vue</td>
<td>整个项目的根组件</td>
</tr>
<tr>
<td>router/index.js</td>
<td>项目中的路由文件</td>
</tr>
<tr>
<td>components</td>
<td>项目中的组件文件</td>
</tr>
<tr>
<td>assets</td>
<td>项目中图片资源</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>config文件夹中的文件</th>
<th>介绍</th>
</tr>
</thead>
<tbody>
<tr>
<td>dev.env.js</td>
<td>开发环境的配置信息</td>
</tr>
<tr>
<td>index.js</td>
<td>基础配置信息</td>
</tr>
<tr>
<td>prod.env.js</td>
<td>线上环境的配置信息</td>
</tr>
</tbody>
</table>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://hatsunem1ku.gitee.io/reimupage/reimupage/2018/05/09/大整数相加/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Reimu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/reimupage/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Reimu'Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/reimupage/2018/05/09/大整数相加/" itemprop="url">实现大整数相加</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-09T09:47:51+08:00">
                2018-05-09
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/reimupage/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="大整数加法"><a href="#大整数加法" class="headerlink" title="大整数加法"></a>大整数加法</h2><p>实现字符串加法<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">   <span class="keyword">var</span> result = [], count = <span class="number">0</span>;</span><br><span class="line">   <span class="keyword">var</span> arrA = a.split(<span class="string">''</span>);</span><br><span class="line">   <span class="keyword">var</span> arrB = b.split(<span class="string">''</span>);</span><br><span class="line">   <span class="keyword">var</span> length = arrA.length &gt; arrB.length ? arrA.length : arrB.length;</span><br><span class="line">   <span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>;i&lt;length;i++)&#123;</span><br><span class="line">      <span class="keyword">var</span> temp = (~~arrA.pop() + ~~arrB.pop()) + count;</span><br><span class="line">      <span class="keyword">if</span>(temp &gt;= <span class="number">10</span>)&#123;</span><br><span class="line">         temp = temp - <span class="number">10</span>;</span><br><span class="line">         count = <span class="number">1</span>;</span><br><span class="line">      &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        count = <span class="number">0</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      result.unshift(temp);</span><br><span class="line">   &#125;</span><br><span class="line">   result.unshift(count);</span><br><span class="line">   <span class="keyword">return</span> result.join(<span class="string">''</span>).replace(<span class="regexp">/^0+/</span>,<span class="string">''</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://hatsunem1ku.gitee.io/reimupage/reimupage/2018/05/07/JSONP/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Reimu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/reimupage/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Reimu'Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/reimupage/2018/05/07/JSONP/" itemprop="url">JSONP 笔记</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-07T15:40:55+08:00">
                2018-05-07
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/reimupage/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="JSONP-笔记"><a href="#JSONP-笔记" class="headerlink" title="JSONP 笔记"></a>JSONP 笔记</h1><hr>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>本系列为学习过程中知识的整理，学习笔记，资料均来源于网络</p>
<h1 id="正文"><a href="#正文" class="headerlink" title="正文"></a>正文</h1><hr>
<h2 id="JSONP原理"><a href="#JSONP原理" class="headerlink" title="JSONP原理"></a>JSONP原理</h2><p>ajax请求受同源策略的影响，不允许进行跨域请求，而script标签src属性中的链接却可以访问跨域的js脚本，利用这个特性，服务端不再返回JSON格式的数据，而是返回一段调用某个函数的js代码，在src中调用，实现跨域。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>JSONP<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">jsonhandle</span>(<span class="params">data</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">      alert(<span class="string">"age:"</span> + data.age + <span class="string">"name:"</span> + data.name);</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined">  </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"jquery-1.8.3.min.js"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"http://www.practice-zhao.com/remote.js"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>服务端remote.js<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jsonhandle(&#123;<span class="string">"age"</span>:<span class="number">15</span>,<span class="string">"name"</span>:<span class="string">"john"</span>&#125;);</span><br></pre></td></tr></table></figure></p>
<p>这段远程的js代码成功执行上面定义的函数，弹出提示框</p>
<p>修改代码如下：<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>JSONP<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">jsonhandle</span>(<span class="params">data</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        alert(<span class="string">"age:"</span> + data.age + <span class="string">"name:"</span> + data.name);</span></span><br><span class="line"><span class="undefined">    &#125;</span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"jquery-1.8.3.min.js"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="javascript">   <span class="function"><span class="keyword">function</span> <span class="title">addScript</span>(<span class="params">src</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">var</span> script = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>);</span></span><br><span class="line"><span class="javascript">      script.setAttribute(<span class="string">"type"</span>,<span class="string">"text/javascript"</span>);</span></span><br><span class="line"><span class="undefined">      script.src = src;</span></span><br><span class="line"><span class="javascript">      <span class="built_in">document</span>.body.appendChild(script);</span></span><br><span class="line"><span class="undefined">   &#125;</span></span><br><span class="line"><span class="javascript">   <span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">      addScript(<span class="string">'http://www.practice-zhao.com/student.php?id=1&amp;callback=jsonhandle'</span>);</span></span><br><span class="line"><span class="undefined">   &#125;</span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>JSONP将访问跨域请求变成了执行远程JS代码，服务端不再返回JSON格式的数据，而是返回了一段将JSON数据作为传入参数的函数执行代码。</p>
<p>script标签请求的脚本，会直接作为代码运行，这时，只要浏览器定义了jsonhandle函数，该函数就会立即调用。作为参数的 JSON 数据被视为 JavaScript 对象，而不是字符串，因此避免了使用JSON.parse的步骤。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://hatsunem1ku.gitee.io/reimupage/reimupage/2018/05/07/String对象/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Reimu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/reimupage/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Reimu'Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/reimupage/2018/05/07/String对象/" itemprop="url">String对象 笔记</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-07T15:40:55+08:00">
                2018-05-07
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/reimupage/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="String对象-笔记"><a href="#String对象-笔记" class="headerlink" title="String对象 笔记"></a>String对象 笔记</h1><hr>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>本系列为学习过程中知识的整理，学习笔记，资料均来源于网络</p>
<h1 id="正文"><a href="#正文" class="headerlink" title="正文"></a>正文</h1><hr>
<h2 id="一、String实例方法"><a href="#一、String实例方法" class="headerlink" title="一、String实例方法"></a>一、String实例方法</h2><h3 id="1-1-String-prototype-charAt"><a href="#1-1-String-prototype-charAt" class="headerlink" title="1.1 String.prototype.charAt()"></a>1.1 String.prototype.charAt()</h3><p>返回指定位置的字符，参数是从0开始编号的位置。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> s = <span class="keyword">new</span> <span class="built_in">String</span>(<span class="string">'abc'</span>);</span><br><span class="line"></span><br><span class="line">s.charAt(<span class="number">1</span>) <span class="comment">// "b"</span></span><br><span class="line">s.charAt(s.length - <span class="number">1</span>) <span class="comment">// "c"</span></span><br></pre></td></tr></table></figure></p>
<p>这个方法可以用数组下标替代。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'abc'</span>.charAt(<span class="number">1</span>) <span class="comment">// "b"</span></span><br><span class="line"><span class="string">'abc'</span>[<span class="number">1</span>] <span class="comment">// "b"</span></span><br></pre></td></tr></table></figure></p>
<p>如果参数为负数，或大于等于字符串的长度，charAt返回空字符串。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'abc'</span>.charAt(<span class="number">-1</span>) <span class="comment">// ""</span></span><br><span class="line"><span class="string">'abc'</span>.charAt(<span class="number">3</span>) <span class="comment">// ""</span></span><br></pre></td></tr></table></figure></p>
<h3 id="1-2-String-prototype-charCodeAt"><a href="#1-2-String-prototype-charCodeAt" class="headerlink" title="1.2 String.prototype.charCodeAt()"></a>1.2 String.prototype.charCodeAt()</h3><p>返回字符串指定位置的Unicode码点（十进制表示），相当于String.fromCharCode()的逆操作。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'abc'</span>.charCodeAt(<span class="number">1</span>)  <span class="comment">//98</span></span><br></pre></td></tr></table></figure></p>
<p>如果没有任何参数，charCodeAt返回首字符的 Unicode 码点。</p>
<p>如果参数为负数，或大于等于字符串的长度，charCodeAt返回NaN。</p>
<h3 id="1-3-String-prototype-concat"><a href="#1-3-String-prototype-concat" class="headerlink" title="1.3 String.prototype.concat()"></a>1.3 String.prototype.concat()</h3><p>用于连接两个字符串，返回一个新字符串，<strong>不改变原字符串</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> s1 = <span class="string">'abc'</span>;</span><br><span class="line"><span class="keyword">var</span> s2 = <span class="string">'def'</span>;</span><br><span class="line">s1.concat(s2) <span class="comment">// "abcdef"</span></span><br><span class="line">s1 <span class="comment">// "abc"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//该方法可以接受多个参数</span></span><br><span class="line"><span class="string">'a'</span>.concat(<span class="string">'b'</span>,<span class="string">'c'</span>)  <span class="comment">//  "abc"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果参数不是字符串，concat会将其先转为字符串，然后连接</span></span><br><span class="line"><span class="keyword">var</span> one = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">var</span> two = <span class="number">2</span>;</span><br><span class="line"><span class="keyword">var</span> three = <span class="string">'3'</span>;</span><br><span class="line"></span><br><span class="line"><span class="string">''</span>.concat(one, two, three) <span class="comment">// "123"</span></span><br><span class="line">one + two + three <span class="comment">// "33"</span></span><br></pre></td></tr></table></figure></p>
<h3 id="1-4-String-prototype-slice"><a href="#1-4-String-prototype-slice" class="headerlink" title="1.4 String.prototype.slice()"></a>1.4 String.prototype.slice()</h3><p>用于从原字符串取出子字符串并返回，<strong>不改变原字符串</strong>，<br>第一个参数是子字符串的开始位置，<br>第二个参数是子字符串的结束位置（不含该位置）。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'JavaScript'</span>.slice(<span class="number">0</span>, <span class="number">4</span>) <span class="comment">// "Java"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果省略第二个参数，则表示子字符串一直到原字符串结束。</span></span><br><span class="line"><span class="string">'JavaScript'</span>.slice(<span class="number">4</span>) <span class="comment">// "Script"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果参数是负值，表示从结尾开始倒数计算的位置，即该负值加上字符串长度。</span></span><br><span class="line"><span class="string">'JavaScript'</span>.slice(<span class="number">-6</span>) <span class="comment">// "Script"</span></span><br><span class="line"><span class="string">'JavaScript'</span>.slice(<span class="number">0</span>, <span class="number">-6</span>) <span class="comment">// "Java"</span></span><br><span class="line"><span class="string">'JavaScript'</span>.slice(<span class="number">-2</span>, <span class="number">-1</span>) <span class="comment">// "p"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果第一个参数大于第二个参数，slice方法返回一个空字符串。</span></span><br><span class="line"><span class="string">'JavaScript'</span>.slice(<span class="number">2</span>, <span class="number">1</span>) <span class="comment">// ""</span></span><br></pre></td></tr></table></figure></p>
<h3 id="1-5-String-prototype-substring"><a href="#1-5-String-prototype-substring" class="headerlink" title="1.5 String.prototype.substring()"></a>1.5 String.prototype.substring()</h3><p>用于从原字符串取出子字符串并返回，<strong>不改变原字符串</strong>，跟<strong>slice</strong>方法很相像。<br>第一个参数表示子字符串的开始位置，第二个位置表示结束位置（返回结果不含该位置）。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'JavaScript'</span>.substring(<span class="number">0</span>, <span class="number">4</span>)  <span class="comment">// "Java"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果省略第二个参数，则表示子字符串一直到原字符串的结束</span></span><br><span class="line"><span class="string">'JavaScript'</span>.substring(<span class="number">4</span>)  <span class="comment">// "Script"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果第二个参数大于第一个参数，substring方法会自动更换两个参数的位置</span></span><br><span class="line"><span class="string">'JavaScript'</span>.substring(<span class="number">10</span>, <span class="number">4</span>) <span class="comment">// "Script"</span></span><br><span class="line"><span class="comment">// 等同于</span></span><br><span class="line"><span class="string">'JavaScript'</span>.substring(<span class="number">4</span>, <span class="number">10</span>) <span class="comment">// "Script"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果参数是负数，substring方法会自动将负数转为0</span></span><br><span class="line"><span class="string">'Javascript'</span>.substring(<span class="number">-3</span>) <span class="comment">// "JavaScript"</span></span><br><span class="line"><span class="string">'JavaScript'</span>.substring(<span class="number">4</span>, <span class="number">-3</span>) <span class="comment">// "Java"</span></span><br></pre></td></tr></table></figure></p>
<h3 id="1-6-String-prototype-substr"><a href="#1-6-String-prototype-substr" class="headerlink" title="1.6 String.prototype.substr()"></a>1.6 String.prototype.substr()</h3><p>用于从原字符串取出子字符串并返回，<strong>不改变原字符串</strong>，跟slice和substring方法的作用相同<br>第一个参数是子字符串的开始位置（从0开始计算），<br>第二个参数是子字符串的长度。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'JavaScript'</span>.substr(<span class="number">4</span>, <span class="number">6</span>) <span class="comment">// "Script"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果省略第二个参数，则表示子字符串一直到原字符串的结束</span></span><br><span class="line"><span class="string">'JavaScript'</span>.substr(<span class="number">4</span>) <span class="comment">// "Script"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果第一个参数是负数，表示倒数计算的字符位置。如果第二个参数是负数，将被自动转为0，因此会返回空字符串。</span></span><br><span class="line"><span class="string">'JavaScript'</span>.substr(<span class="number">-6</span>) <span class="comment">// "Script"</span></span><br><span class="line"><span class="string">'JavaScript'</span>.substr(<span class="number">4</span>, <span class="number">-1</span>) <span class="comment">// ""</span></span><br></pre></td></tr></table></figure></p>
<h3 id="1-7-String-prototype-indexOf-，String-prototype-lastIndexOf"><a href="#1-7-String-prototype-indexOf-，String-prototype-lastIndexOf" class="headerlink" title="1.7 String.prototype.indexOf()，String.prototype.lastIndexOf()"></a>1.7 String.prototype.indexOf()，String.prototype.lastIndexOf()</h3><p>indexOf方法用于确定一个字符串在另一个字符串中<strong>第一次</strong>出现的位置，返回结果是匹配开始的位置，如果返回-1，就表示不匹配<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'hello world'</span>.indexOf(<span class="string">'o'</span>) <span class="comment">// 4</span></span><br><span class="line"><span class="string">'JavaScript'</span>.indexOf(<span class="string">'script'</span>) <span class="comment">// -1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//indexOf方法还可以接受第二个参数，表示从该位置开始向后匹配</span></span><br><span class="line"><span class="string">'hello world'</span>.indexOf(<span class="string">'o'</span>, <span class="number">6</span>) <span class="comment">// 7</span></span><br></pre></td></tr></table></figure></p>
<p>lastIndexOf方法的用法跟indexOf方法一致，主要区别是lastIndexOf从尾部开始匹配，indexOf则是从头部开始匹配。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'hello world'</span>.lastIndexOf(<span class="string">'o'</span>) <span class="comment">// 7</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//lastIndexOf的第二个参数表示从该位置起向前匹配</span></span><br><span class="line"><span class="string">'hello world'</span>.lastIndexOf(<span class="string">'o'</span>, <span class="number">6</span>) <span class="comment">// 4</span></span><br></pre></td></tr></table></figure></p>
<h3 id="1-8-String-prototype-trim"><a href="#1-8-String-prototype-trim" class="headerlink" title="1.8 String.prototype.trim()"></a>1.8 String.prototype.trim()</h3><p>用于去除字符串两端的空格，返回一个新字符串，<strong>不改变原字符串</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'  hello world  '</span>.trim()  <span class="comment">// "hello world"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//该方法去除的不仅是空格，还包括制表符（\t、\v）、换行符（\n）和回车符（\r）</span></span><br></pre></td></tr></table></figure></p>
<h3 id="1-9-String-prototype-toLowerCase-，String-prototype-toUpperCase"><a href="#1-9-String-prototype-toLowerCase-，String-prototype-toUpperCase" class="headerlink" title="1.9 String.prototype.toLowerCase()，String.prototype.toUpperCase()"></a>1.9 String.prototype.toLowerCase()，String.prototype.toUpperCase()</h3><p>toLowerCase方法用于将一个字符串全部转为小写，<br>toUpperCase则是全部转为大写。<br>它们都返回一个新字符串，<strong>不改变原字符串</strong>。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'Hello World'</span>.toLowerCase()</span><br><span class="line"><span class="comment">// "hello world"</span></span><br><span class="line"></span><br><span class="line"><span class="string">'Hello World'</span>.toUpperCase()</span><br><span class="line"><span class="comment">// "HELLO WORLD"</span></span><br></pre></td></tr></table></figure></p>
<h3 id="2-0-String-prototype-match"><a href="#2-0-String-prototype-match" class="headerlink" title="2.0 String.prototype.match()"></a>2.0 String.prototype.match()</h3><p>用于确定原字符串是否匹配某个子字符串，返回一个数组，成员为<strong>匹配的第一个字符串</strong>，如果没有匹配，则返回null。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'cat, bat, sat, fat'</span>.match(<span class="string">'at'</span>) <span class="comment">// ["at"]</span></span><br><span class="line"><span class="string">'cat, bat, sat, fat'</span>.match(<span class="string">'xt'</span>) <span class="comment">// null</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//返回的数组还有index属性和input属性，分别表示匹配字符串开始的位置和原始字符串</span></span><br><span class="line"><span class="keyword">var</span> matches = <span class="string">'cat, bat, sat, fat'</span>.match(<span class="string">'at'</span>);</span><br><span class="line">matches.index <span class="comment">// 1</span></span><br><span class="line">matches.input <span class="comment">// "cat, bat, sat, fat"</span></span><br></pre></td></tr></table></figure></p>
<h3 id="2-1-String-prototype-search-，String-prototype-replace"><a href="#2-1-String-prototype-search-，String-prototype-replace" class="headerlink" title="2.1 String.prototype.search()，String.prototype.replace()"></a>2.1 String.prototype.search()，String.prototype.replace()</h3><p>search方法的用法基本等同于match，但是返回值为匹配的第一个位置。如果没有找到匹配，则返回-1。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'cat, bat, sat, fat'</span>.search(<span class="string">'at'</span>) <span class="comment">// 1</span></span><br></pre></td></tr></table></figure></p>
<p>replace方法用于替换匹配的子字符串，一般情况下只替换第一个匹配（除非使用带有g修饰符的正则表达式）。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'aaa'</span>.replace(<span class="string">'a'</span>, <span class="string">'b'</span>) <span class="comment">// "baa"</span></span><br></pre></td></tr></table></figure></p>
<h3 id="2-2-String-prototype-split"><a href="#2-2-String-prototype-split" class="headerlink" title="2.2 String.prototype.split()"></a>2.2 String.prototype.split()</h3><p>split方法按照给定规则分割字符串，返回一个由分割出来的子字符串组成的数组。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'a|b|c'</span>.split(<span class="string">'|'</span>)   <span class="comment">// ["a", "b", "c"]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果分隔规则为空字符串，则返回原字符串的每一个字符</span></span><br><span class="line"><span class="string">'a|b|c'</span>.split(<span class="string">''</span>) <span class="comment">// ["a", "|", "b", "|", "c"]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果省略参数，则返回数组的唯一成员就是原字符串</span></span><br><span class="line"><span class="string">'a|b|c'</span>.split() <span class="comment">// ["a|b|c"]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果满足分割规则的两个部分紧邻着（即两个分割符中间没有其他字符），则返回数组之中会有一个空字符串</span></span><br><span class="line"><span class="string">'a||c'</span>.split(<span class="string">'|'</span>) <span class="comment">// ['a', '', 'c']</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果满足分割规则的部分处于字符串的开头或结尾（即它的前面或后面没有其他字符），则返回数组的第一个或最后一个成员是一个空字符串</span></span><br><span class="line"><span class="string">'|b|c'</span>.split(<span class="string">'|'</span>) <span class="comment">// ["", "b", "c"]</span></span><br><span class="line"><span class="string">'a|b|'</span>.split(<span class="string">'|'</span>) <span class="comment">// ["a", "b", ""]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//split方法还可以接受第二个参数，限定返回数组的最大成员数</span></span><br><span class="line"><span class="string">'a|b|c'</span>.split(<span class="string">'|'</span>, <span class="number">0</span>) <span class="comment">// []</span></span><br><span class="line"><span class="string">'a|b|c'</span>.split(<span class="string">'|'</span>, <span class="number">1</span>) <span class="comment">// ["a"]</span></span><br><span class="line"><span class="string">'a|b|c'</span>.split(<span class="string">'|'</span>, <span class="number">2</span>) <span class="comment">// ["a", "b"]</span></span><br><span class="line"><span class="string">'a|b|c'</span>.split(<span class="string">'|'</span>, <span class="number">3</span>) <span class="comment">// ["a", "b", "c"]</span></span><br><span class="line"><span class="string">'a|b|c'</span>.split(<span class="string">'|'</span>, <span class="number">4</span>) <span class="comment">// ["a", "b", "c"]</span></span><br></pre></td></tr></table></figure></p>
<h3 id="2-3-String-prototype-localeCompare"><a href="#2-3-String-prototype-localeCompare" class="headerlink" title="2.3 String.prototype.localeCompare()"></a>2.3 String.prototype.localeCompare()</h3><p>用于比较两个字符串。它返回一个整数，<br>如果小于0，表示第一个字符串小于第二个字符串；<br>如果等于0，表示两者相等；<br>如果大于0，表示第一个字符串大于第二个字符串。</p>
<h2 id="二、String实例属性"><a href="#二、String实例属性" class="headerlink" title="二、String实例属性"></a>二、String实例属性</h2><p>String.prototype.length<br>返回字符串的长度<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'abc'</span>.length  <span class="comment">//3</span></span><br></pre></td></tr></table></figure></p>
<h2 id="三、String静态方法"><a href="#三、String静态方法" class="headerlink" title="三、String静态方法"></a>三、String静态方法</h2><p>String.fromCharCode()<br>该方法参数是一个或多个数值，代表Unicode码点，返回值是这些码点组成的字符串。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">String</span>.fromCharCode()  <span class="comment">// ""</span></span><br><span class="line"><span class="built_in">String</span>.fromCharCode(<span class="number">97</span>)  <span class="comment">// "a"</span></span><br><span class="line"><span class="built_in">String</span>.fromCharCode(<span class="number">104</span>, <span class="number">101</span>, <span class="number">108</span>, <span class="number">108</span>, <span class="number">111</span>)  <span class="comment">// "hello"</span></span><br></pre></td></tr></table></figure></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://hatsunem1ku.gitee.io/reimupage/reimupage/2018/05/06/RegExp对象/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Reimu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/reimupage/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Reimu'Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/reimupage/2018/05/06/RegExp对象/" itemprop="url">正则表达式 笔记</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-06T15:33:33+08:00">
                2018-05-06
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/reimupage/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="正则表达式-笔记"><a href="#正则表达式-笔记" class="headerlink" title="正则表达式 笔记"></a>正则表达式 笔记</h1><hr>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>本系列为学习过程中知识的整理，学习笔记，资料均来源于网络</p>
<h1 id="正文"><a href="#正文" class="headerlink" title="正文"></a>正文</h1><hr>
<p>正则表达式的创建有两种方式：<strong>new RegExp(expression)</strong>和<strong>直接字面量</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用直接字面量创建</span></span><br><span class="line"><span class="keyword">var</span> exp1 = <span class="regexp">/pattern/</span>flags;</span><br><span class="line"><span class="keyword">var</span> exp1 = <span class="regexp">/(^\s+)|(\s+$)/g</span>;</span><br><span class="line"><span class="comment">//使用RegExp对象创建</span></span><br><span class="line"><span class="keyword">var</span> exp2 = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">'pattern'</span>,<span class="string">'flags'</span>);</span><br><span class="line"><span class="keyword">var</span> exp2 = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">'(^\\s+)|(\\s+$)'</span>,<span class="string">'g'</span>);</span><br></pre></td></tr></table></figure></p>
<p>每个正则表达式都可带有一个或多个标志(flags)，用以标明正则表达式的行为，正则表达式支持下列3个标志：</p>
<blockquote>
<ul>
<li>g：表示全局（global）模式，即模式将被应用于所有字符串，而非在发现第一个匹配项时立即停止；</li>
<li>i：表示不区分大小写（case-insensitive）模式，即在确定匹配项时忽略模式与字符串的大小写；</li>
<li>m：表示多行（multiline）模式，即在到达一行文本末尾时还会继续查找下一行中是否存在与模式匹配的项。</li>
</ul>
</blockquote>
<h1 id="正则表达式的元字符"><a href="#正则表达式的元字符" class="headerlink" title="正则表达式的元字符"></a>正则表达式的元字符</h1><h2 id="1"><a href="#1" class="headerlink" title="1. ()  []  {}"></a>1. ()  []  {}</h2><p>()：<strong>的作用是提取匹配的字符串</strong>。表达式中有几个()就会得到几个相应的匹配字符串。比如 (\s+) 表示连续空格的字符串。</p>
<p>[]：<strong>是定义匹配的字符范围</strong>。比如 [a-zA-Z0-9] 表示字符文本要匹配英文字符和数字。</p>
<p>{}：<strong>一般用来表示匹配的长度</strong>，比如 \d{3} 表示匹配三个数字，\d{1,3} 表示匹配1~3个数字，\d{3,} 表示匹配3个以上数字。</p>
<h2 id="2"><a href="#2" class="headerlink" title="2. ^  $"></a>2. ^  $</h2><p>^：<strong>匹配一个字符串的开头</strong>，比如(^a)就是匹配以字母a开头的字符串</p>
<p>\$：<strong>匹配一个字符串的结尾</strong>，比如(b$)就是匹配以字母b结尾的字符串</p>
<p>^ 还有另个一个作用就是取反，比如[^xyz]表示匹配的字符串不包含xyz<br><strong>注意：</strong><br>如果<strong>^ 出现在[ ]中一般表示取反</strong>，而出现在其他地方则是匹配字符串的开头。</p>
<h2 id="3-d-s-w"><a href="#3-d-s-w" class="headerlink" title="3. \d   \s   \w   ."></a>3. \d   \s   \w   .</h2><p>\d：匹配一个非负整数， 等价于 [0-9]</p>
<p>\s：匹配一个空白字符</p>
<p>\w：匹配一个英文字母或数字，等价于[0-9a-zA-Z]</p>
<p>. ：匹配除换行符以外的任意字符，等价于[^\n]</p>
<h2 id="4"><a href="#4" class="headerlink" title="4. *  +  ?"></a>4. *  +  ?</h2><p>* ：表示匹配前面元素0次或多次，比如 (\s*)就是匹配0个或多个空格</p>
<p>+ ：表示匹配前面元素1次或多次，比如 (\d+)就是匹配由至少1个整数组成的字符串</p>
<p>? ：表示匹配前面元素0次或1次，相当于{0,1} ，比如(\w?)就是匹配最多由1个字母或数字组成的字符串 </p>
<h2 id="5-1-1"><a href="#5-1-1" class="headerlink" title="5. $1  \1"></a>5. $1  \1</h2><p>\$1-\$9 存放着正则表达式中最近的9个正则表达式的提取的结果，这些结果按照子匹配的出现顺序依次排列。基本语法是：RegExp.$n ，这些属性是静态的，除了replace中的第二个参数可以省略 RegExp 之外，其他地方使用都要加上 RegExp。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用RegExp访问</span></span><br><span class="line">/(\d+)-(\d+)-(\d+)/.test(<span class="string">'2016-03-26'</span>)</span><br><span class="line"><span class="built_in">RegExp</span>.$<span class="number">1</span> <span class="comment">//2016</span></span><br><span class="line"><span class="built_in">RegExp</span>.$<span class="number">2</span> <span class="comment">//03</span></span><br><span class="line"><span class="built_in">RegExp</span>.$<span class="number">3</span> <span class="comment">//26</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//在replace中使用</span></span><br><span class="line"><span class="string">'2016-03-26'</span>.replace(<span class="regexp">/(\d+)-(\d+)-(\d+)/</span>,<span class="string">'$1年$2月$3日'</span>)</span><br><span class="line"><span class="comment">// 2016年03月26日</span></span><br></pre></td></tr></table></figure>
<p>\1  表示后向引用，是指在正则表达式中，从左往右数，第1个()中的内容，以此类推，\2表示第2个()，\0表示整个表达式。</p>
<p>其后面出现\1则是代表与第一个小括号中要匹配的内容相同<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//匹配日期格式，表达式中的\1代表重复(\-|\/|.)</span></span><br><span class="line"><span class="keyword">var</span> rgx = <span class="regexp">/\d&#123;4&#125;(\-|\/|.)\d&#123;1,2&#125;\1\d&#123;1,2&#125;/</span></span><br><span class="line"></span><br><span class="line">rgx.test(<span class="string">'2016-03-26'</span>) <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line">rgx.test(<span class="string">'2016-03.26'</span>) <span class="comment">//false</span></span><br></pre></td></tr></table></figure></p>
<p><strong>两者的区别：</strong>\n只能用在表达式中，而$n只能用在表达式之外的地方</p>
<h1 id="6-RegExp-prototype-test-String-prototype-match"><a href="#6-RegExp-prototype-test-String-prototype-match" class="headerlink" title="6. RegExp.prototype.test()   String.prototype.match()"></a>6. RegExp.prototype.test()   String.prototype.match()</h1><p>test：用来检测字符串是否匹配某一个正则表达式，如果匹配就会返回true，反之则返回false<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">/\d+<span class="regexp">/.test('123'); /</span><span class="regexp">/true</span></span><br><span class="line"><span class="regexp">/</span>\d+<span class="regexp">/.test('abc'); /</span><span class="regexp">/false</span></span><br></pre></td></tr></table></figure></p>
<p>match：是获取正则匹配到的结果，以数组的形式返回<br>‘186a619b28’.match(/\d+/g); //[‘186’,’619’,’28’]</p>
<h1 id="7-String-prototype-replace"><a href="#7-String-prototype-replace" class="headerlink" title="7. String.prototype.replace()"></a>7. String.prototype.replace()</h1><p>replace 本身是JavaScript字符串对象的一个方法，它允许接收两个参数：<br><strong>str.replace([RegExp|String], [String|Function])</strong><br>参数1：可以是一个普通的字符串或是一个正则表达式<br>参数2：可以是一个普通的字符串或是一个回调函数</p>
<h2 id="7-1-实现字符串trim函数，去除字符串两边的空格"><a href="#7-1-实现字符串trim函数，去除字符串两边的空格" class="headerlink" title="7.1 实现字符串trim函数，去除字符串两边的空格"></a>7.1 实现字符串trim函数，去除字符串两边的空格</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">String</span>.prototype.trim = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">   <span class="comment">//方式一：将匹配到的每一个结果都用''替换</span></span><br><span class="line">   <span class="keyword">return</span> <span class="keyword">this</span>.replace(<span class="regexp">/(^\s+)|(\s+$)/g</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="string">""</span>;</span><br><span class="line">   &#125;);</span><br><span class="line">   <span class="comment">//方式二</span></span><br><span class="line">   <span class="keyword">return</span> <span class="keyword">this</span>.replace(<span class="regexp">/(^\s+)|(\s+$)/</span>,<span class="string">""</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="7-2-提取浏览器url中的参数名和参数值"><a href="#7-2-提取浏览器url中的参数名和参数值" class="headerlink" title="7.2 提取浏览器url中的参数名和参数值"></a>7.2 提取浏览器url中的参数名和参数值</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getUrlParamObj</span>(<span class="params">url</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">var</span> obj = &#123;&#125;;</span><br><span class="line">  url.replace(<span class="regexp">/([^?&amp;=]+)=([^&amp;=]*)/gi</span>,<span class="function"><span class="keyword">function</span>(<span class="params">rs,$<span class="number">1</span>,$<span class="number">2</span></span>)</span>&#123;</span><br><span class="line">     obj[$<span class="number">1</span>] = $<span class="number">2</span>;</span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="keyword">return</span> obj;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>作为replace方法第二个参数的替换函数，可以接受多个参数：</p>
<p>第一个参数是捕捉到的内容；<br>第二个参数是捕捉到的组匹配（有多少个组匹配，就有多少个对应的参数）；<br>此外，最后还可以添加两个参数，<br>倒数第二个参数是捕捉到的内容在整个字符串中的位置（比如从第五个位置开始）；<br>最后一个参数是原字符串。</p>
<h1 id="8-RegExp-prototype-exec"><a href="#8-RegExp-prototype-exec" class="headerlink" title="8. RegExp.prototype.exec()"></a>8. RegExp.prototype.exec()</h1><p>用来返回匹配结果，如果发现匹配，就返回一个数组，成员是匹配成功的子字符串，否则返回null。<br>exec方法的返回数组还包含以下两个属性：</p>
<blockquote>
<ul>
<li>input：整个原字符串</li>
<li>index：整个模式匹配成功的开始位置(从0开始计数)<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> r = <span class="regexp">/a(b+)a/</span>;</span><br><span class="line"><span class="keyword">var</span> arr = r.exec(<span class="string">'_abbba_aba_'</span>);</span><br><span class="line"></span><br><span class="line">arr <span class="comment">// ["abbba", "bbb"]</span></span><br><span class="line"></span><br><span class="line">arr.index <span class="comment">// 1</span></span><br><span class="line">arr.input <span class="comment">// "_abbba_aba_"</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</blockquote>
<p>如果正则表达式加上g修饰符，则可以使用多次exec方法，下一次搜索的位置从上一次匹配成功结束的位置开始。利用g修饰符允许多次匹配的特点，可以用一个循环完成全部匹配。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/a/g</span>;</span><br><span class="line"><span class="keyword">var</span> str = <span class="string">'abc_abc_abc'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(<span class="literal">true</span>) &#123;</span><br><span class="line">  <span class="keyword">var</span> match = reg.exec(str);</span><br><span class="line">  <span class="keyword">if</span> (!match) <span class="keyword">break</span>;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'#'</span> + match.index + <span class="string">':'</span> + match[<span class="number">0</span>]);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// #0:a</span></span><br><span class="line"><span class="comment">// #4:a</span></span><br><span class="line"><span class="comment">// #8:a</span></span><br></pre></td></tr></table></figure></p>
<h1 id="9-String-prototype-search"><a href="#9-String-prototype-search" class="headerlink" title="9. String.prototype.search()"></a>9. String.prototype.search()</h1><p>字符串对象的search方法，返回第一个满足条件的匹配结果在整个字符串中的位置。如果没有任何匹配，则返回-1。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'_x_x'</span>.search(<span class="regexp">/x/</span>);</span><br><span class="line"><span class="comment">// 1</span></span><br></pre></td></tr></table></figure></p>
<h1 id="10-String-prototype-split"><a href="#10-String-prototype-split" class="headerlink" title="10. String.prototype.split()"></a>10. String.prototype.split()</h1><p>字符串对象的split方法按照正则规则分割字符串，返回一个由分割后的各个部分组成的数组。</p>
<p><strong>str.split(separator, [limit])</strong><br>第一个参数是正则表达式，表示分隔规则，第二个参数是返回数组的最大成员数。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 非正则分隔</span></span><br><span class="line"><span class="string">'a,  b,c, d'</span>.split(<span class="string">','</span>)</span><br><span class="line"><span class="comment">// [ 'a', '  b', 'c', ' d' ]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 正则分隔，去除多余的空格</span></span><br><span class="line"><span class="string">'a,  b,c, d'</span>.split(<span class="regexp">/, */</span>)</span><br><span class="line"><span class="comment">// [ 'a', 'b', 'c', 'd' ]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 指定返回数组的最大成员</span></span><br><span class="line"><span class="string">'a,  b,c, d'</span>.split(<span class="regexp">/, */</span>, <span class="number">2</span>)</span><br><span class="line">[ <span class="string">'a'</span>, <span class="string">'b'</span> ]</span><br></pre></td></tr></table></figure></p>
<h1 id="11-贪婪模式"><a href="#11-贪婪模式" class="headerlink" title="11. 贪婪模式"></a>11. 贪婪模式</h1><p>将贪婪模式改为非贪婪模式，可以在量词符后面加一个问号<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> s = <span class="string">'aaa'</span>;</span><br><span class="line">s.match(<span class="regexp">/a+?/</span>) <span class="comment">// ["a"]</span></span><br></pre></td></tr></table></figure></p>
<blockquote>
<ul>
<li>*?：表示某个模式出现0次或多次，匹配时采用非贪婪模式。</li>
<li>+?：表示某个模式出现1次或多次，匹配时采用非贪婪模式。</li>
</ul>
</blockquote>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://hatsunem1ku.gitee.io/reimupage/reimupage/2018/04/29/Array对象/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Reimu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/reimupage/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Reimu'Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/reimupage/2018/04/29/Array对象/" itemprop="url">Array对象 笔记</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-04-29T17:08:38+08:00">
                2018-04-29
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/reimupage/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="Array对象-笔记"><a href="#Array对象-笔记" class="headerlink" title="Array对象 笔记"></a>Array对象 笔记</h1><hr>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>本系列为学习过程中知识的整理，学习笔记，资料均来源于网络</p>
<h1 id="正文"><a href="#正文" class="headerlink" title="正文"></a>正文</h1><hr>
<h2 id="一、Array构造函数"><a href="#一、Array构造函数" class="headerlink" title="一、Array构造函数"></a>一、Array构造函数</h2><p>Array是JavaScript的原生对象，同时也是一个构造函数，可以生成新数组。不同的参数，Array的行为也不同：</p>
<h3 id="1-无参数-返回空数组"><a href="#1-无参数-返回空数组" class="headerlink" title="1.无参数 返回空数组"></a>1.无参数 返回空数组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = <span class="keyword">new</span> <span class="built_in">Array</span>();  <span class="comment">//[]</span></span><br></pre></td></tr></table></figure>
<h3 id="2-单个正整数参数-表示数组长度"><a href="#2-单个正整数参数-表示数组长度" class="headerlink" title="2.单个正整数参数 表示数组长度"></a>2.单个正整数参数 表示数组长度</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">5</span>);  <span class="comment">//[empty × 5]</span></span><br><span class="line"><span class="keyword">var</span> arr1 = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">1.0</span>);  <span class="comment">//[empty]</span></span><br></pre></td></tr></table></figure>
<h3 id="3-非正整数参数-报错"><a href="#3-非正整数参数-报错" class="headerlink" title="3.非正整数参数 报错"></a>3.非正整数参数 报错</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">1.1</span>);  <span class="comment">//Uncaught RangeError: Invalid array length</span></span><br><span class="line"><span class="keyword">var</span> arr1 = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">-1</span>);  <span class="comment">// Uncaught RangeError: Invalid array length</span></span><br></pre></td></tr></table></figure>
<h3 id="4-单个非数值-该参数为新数组成员"><a href="#4-单个非数值-该参数为新数组成员" class="headerlink" title="4.单个非数值 该参数为新数组成员"></a>4.单个非数值 该参数为新数组成员</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="literal">false</span>);  <span class="comment">// [false]</span></span><br><span class="line"><span class="keyword">var</span> arr1 = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="string">'abc'</span>);  <span class="comment">//  ["abc"]</span></span><br><span class="line"><span class="keyword">var</span> arr2 = <span class="keyword">new</span> <span class="built_in">Array</span>([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]); <span class="comment">// [Array(3)]</span></span><br></pre></td></tr></table></figure>
<h3 id="5-多参数时，所有参数都是新数组成员"><a href="#5-多参数时，所有参数都是新数组成员" class="headerlink" title="5.多参数时，所有参数都是新数组成员"></a>5.多参数时，所有参数都是新数组成员</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>);  <span class="comment">// [1, 2, 3, 4]</span></span><br><span class="line"><span class="keyword">var</span> arr1 = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>);  <span class="comment">//  ["a", "b", "c"]</span></span><br></pre></td></tr></table></figure>
<table>
<thead>
<tr>
<th>参数种类</th>
<th style="text-align:right">例子</th>
<th style="text-align:center">行为</th>
</tr>
</thead>
<tbody>
<tr>
<td>无参数</td>
<td style="text-align:right">Array()</td>
<td style="text-align:center">空数组</td>
</tr>
<tr>
<td>单个正整数</td>
<td style="text-align:right">Array(2)</td>
<td style="text-align:center">表示返回新数组的长度</td>
</tr>
<tr>
<td>非正整数</td>
<td style="text-align:right">Array(-1)</td>
<td style="text-align:center">报错</td>
</tr>
<tr>
<td>单个非数组</td>
<td style="text-align:right">Array(true)</td>
<td style="text-align:center">[true]</td>
</tr>
<tr>
<td>多参数</td>
<td style="text-align:right">Array(1,2,3,4)</td>
<td style="text-align:center">[1,2,3,4]</td>
</tr>
</tbody>
</table>
<h2 id="二、Array对象属性"><a href="#二、Array对象属性" class="headerlink" title="二、Array对象属性"></a>二、Array对象属性</h2><h3 id="1-Array-isArray"><a href="#1-Array-isArray" class="headerlink" title="1. Array.isArray()"></a>1. Array.isArray()</h3><p>用于判断一个对象是否为数组，返回true或false<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line"><span class="keyword">typeof</span> arr;   <span class="comment">//"object"</span></span><br><span class="line"><span class="built_in">Array</span>.isArray(arr);  <span class="comment">//true</span></span><br></pre></td></tr></table></figure></p>
<hr>
<h3 id="2-valueOf-，toString"><a href="#2-valueOf-，toString" class="headerlink" title="2. valueOf()，toString()"></a>2. valueOf()，toString()</h3><p>valueOf()：返回数组对象的原始值<br>toString()：把数组转换为字符串，并返回结果<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line">arr.valueOf();  <span class="comment">// [1, 2, 3]</span></span><br><span class="line">arr.toString(); <span class="comment">// "1,2,3"</span></span><br></pre></td></tr></table></figure></p>
<hr>
<h3 id="3-push-，pop"><a href="#3-push-，pop" class="headerlink" title="3. push()，pop()"></a>3. push()，pop()</h3><p>push()：向数组的末尾添加一个或更多元素，并返回新的长度 <strong>会改变原数组</strong><br>pop()：删除数组的最后一个元素并返回该元素 <strong>会改变原数组</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line">arr.push(<span class="string">"a"</span>); <span class="comment">//4</span></span><br><span class="line">arr <span class="comment">//[1, 2, 3, "a"]</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line">arr.pop(); <span class="comment">//3</span></span><br><span class="line">arr <span class="comment">// [1, 2]</span></span><br></pre></td></tr></table></figure>
<p>push和pop结合使用，构成<strong>后进先出的栈结构</strong></p>
<hr>
<h3 id="4-shift-，unshift"><a href="#4-shift-，unshift" class="headerlink" title="4. shift()，unshift()"></a>4. shift()，unshift()</h3><p>shift()：删除并返回数组的第一个元素 <strong>会改变原数组</strong><br>unshift()：向数组的开头添加一个或更多元素，并返回新的长度 <strong>会改变原数组</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>];</span><br><span class="line">arr.shift();  <span class="comment">// 1</span></span><br><span class="line">arr  <span class="comment">// [2, 3, 4]</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>];</span><br><span class="line">arr.unshift(<span class="number">1</span>); <span class="comment">//  4</span></span><br><span class="line">arr  <span class="comment">//  [1, "a", "b", "c"]</span></span><br></pre></td></tr></table></figure>
<p>push和shift结合使用，就构成了<strong>先进先出的队列结构</strong></p>
<hr>
<h3 id="5-slice-，splice"><a href="#5-slice-，splice" class="headerlink" title="5. slice()，splice()"></a>5. slice()，splice()</h3><p>slice()：选取数组的一部分，并返回一个新数组 <strong>原数组不变</strong><br>splice()：从数组中删除或添加元素，返回删除的元素 <strong>会改变原数组</strong></p>
<hr>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">arr.slice(start,end);</span><br></pre></td></tr></table></figure>
<p>第一个参数为起始位置<strong>从0开始</strong>，第二个参数为终止位置<strong>该位置的元素不包括在内</strong>，如果省略第二个元素，则一直提取到原数组末尾；如果第二个元素大于数组长度，也会一直提取到原数组末尾。</p>
<p>如果第一个参数大于等于数组长度，或者第二个参数小于第一个参数，则返回空数组<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>];</span><br><span class="line">arr.slice(<span class="number">0</span>); <span class="comment">//  [1, 2, 3, 4, 5, 6]</span></span><br><span class="line">arr.slice(<span class="number">1</span>); <span class="comment">//  [2, 3, 4, 5, 6]</span></span><br><span class="line">arr.slice(<span class="number">1</span>,<span class="number">6</span>); <span class="comment">//  [2, 3, 4, 5, 6]</span></span><br><span class="line">arr.slice(); <span class="comment">//  [1, 2, 3, 4, 5, 6]  复制一个数组</span></span><br><span class="line"></span><br><span class="line">arr.slice(<span class="number">3</span>,<span class="number">2</span>); <span class="comment">// []</span></span><br></pre></td></tr></table></figure></p>
<p>如果参数为负数，则表示倒数计算的位置<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>];</span><br><span class="line">arr.slice(<span class="number">-1</span>); <span class="comment">//  [6]  -1表示倒数第一的位置</span></span><br><span class="line">arr.slice(<span class="number">-2</span>,<span class="number">-1</span>); <span class="comment">// [5]  -2表示倒数第二的位置</span></span><br><span class="line"></span><br><span class="line">arr.slice(<span class="number">-1</span>,<span class="number">-2</span>);  <span class="comment">// []</span></span><br></pre></td></tr></table></figure></p>
<hr>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">array.splice(start,count,item1,.....,itemX);</span><br></pre></td></tr></table></figure>
<p>第一个参数是删除的起始位置<strong>从0开始</strong>，第二个参数是被删除的元素个数。如果后面还有更多的参数，则表示要被插入数组的新元素。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'d'</span>, <span class="string">'e'</span>, <span class="string">'f'</span>];</span><br><span class="line">arr.splice(<span class="number">4</span>, <span class="number">2</span>);  <span class="comment">//  ["e", "f"]</span></span><br><span class="line">arr  <span class="comment">//  ["a", "b", "c", "d"]</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'d'</span>, <span class="string">'e'</span>, <span class="string">'f'</span>];</span><br><span class="line">arr.splice(<span class="number">4</span>, <span class="number">2</span>, <span class="string">"g"</span>, <span class="string">"h"</span>, <span class="number">1</span>, <span class="number">2</span>);  <span class="comment">//  ["e", "f"]</span></span><br><span class="line">arr  <span class="comment">//  ["a", "b", "c", "d", "g", "h", 1, 2]</span></span><br></pre></td></tr></table></figure>
<p>起始位置如果是负数，就表示从倒数位置开始删除<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'d'</span>, <span class="string">'e'</span>, <span class="string">'f'</span>];</span><br><span class="line">arr.splice(<span class="number">-3</span>,<span class="number">2</span>);  <span class="comment">//   ["d", "e"]</span></span><br></pre></td></tr></table></figure></p>
<p>如果只是单纯地插入元素，splice方法的第二个参数可以设为0。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'d'</span>, <span class="string">'e'</span>, <span class="string">'f'</span>];</span><br><span class="line">arr.splice(<span class="number">1</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">3</span>); <span class="comment">// []</span></span><br><span class="line">arr  <span class="comment">// ["a", 2, 3, "b", "c", "d", "e", "f"]</span></span><br></pre></td></tr></table></figure></p>
<p>如果只提供一个参数，等同于将原数组在指定位置拆分成两个数组。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'d'</span>, <span class="string">'e'</span>, <span class="string">'f'</span>];</span><br><span class="line">arr.splice(<span class="number">4</span>); <span class="comment">// ["e", "f"]</span></span><br><span class="line">arr  <span class="comment">//  ["a", "b", "c", "d"]</span></span><br></pre></td></tr></table></figure></p>
<hr>
<h3 id="6-join"><a href="#6-join" class="headerlink" title="6. join()"></a>6. join()</h3><p>把数组中的所有元素转换为一个字符串,如果不提供参数，默认用逗号分隔。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</span><br><span class="line">arr.join(); <span class="comment">//  "1,2,3,4,5"</span></span><br><span class="line">arr.join(<span class="string">' '</span>);  <span class="comment">//  "1 2 3 4 5"</span></span><br><span class="line">arr.join(<span class="string">'|'</span>); <span class="comment">//  "1|2|3|4|5"</span></span><br><span class="line">arr.join(<span class="string">'and'</span>);  <span class="comment">//  "1and2and3and4and5"</span></span><br></pre></td></tr></table></figure></p>
<p>如果数组成员是undefined或null或空位，会被转成空字符串。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[<span class="literal">undefined</span>, <span class="literal">null</span>].join(<span class="string">'#'</span>)  <span class="comment">// '#'</span></span><br><span class="line">[<span class="string">'a'</span>,, <span class="string">'b'</span>].join(<span class="string">'-'</span>)  <span class="comment">// 'a--b'</span></span><br></pre></td></tr></table></figure></p>
<hr>
<h3 id="6-concat"><a href="#6-concat" class="headerlink" title="6. concat()"></a>6. concat()</h3><p>连接两个或多个数组，将新数组的成员，添加到原数组成员的后部，然后返回一个新数组，<strong>原数组不变</strong>。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[<span class="string">'hello'</span>].concat([<span class="string">'world'</span>])  <span class="comment">// ["hello", "world"]</span></span><br><span class="line"></span><br><span class="line">[<span class="string">'hello'</span>].concat([<span class="string">'world'</span>], [<span class="string">'!'</span>])  <span class="comment">// ["hello", "world", "!"]</span></span><br><span class="line"></span><br><span class="line">[].concat(&#123;<span class="attr">a</span>: <span class="number">1</span>&#125;, &#123;<span class="attr">b</span>: <span class="number">2</span>&#125;)  <span class="comment">// [&#123; a: 1 &#125;, &#123; b: 2 &#125;]</span></span><br><span class="line"></span><br><span class="line">[<span class="number">2</span>].concat(&#123;<span class="attr">a</span>: <span class="number">1</span>&#125;)  <span class="comment">// [2, &#123;a: 1&#125;]</span></span><br></pre></td></tr></table></figure></p>
<p>如果数组成员包括<strong>对象引用</strong>，concat方法返回当前数组的一个<strong>浅拷贝</strong>。所谓“浅拷贝”，指的是新数组拷贝的是对象的引用。对新数组操作会对原始数组产生影响。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123; <span class="attr">a</span>: <span class="number">1</span> &#125;;</span><br><span class="line"><span class="keyword">var</span> oldArr = [obj];</span><br><span class="line"><span class="keyword">var</span> newArr = oldArr.concat();  <span class="comment">// [&#123;a:1&#125;]</span></span><br><span class="line">obj.a = <span class="number">2</span>;</span><br><span class="line">newArr[<span class="number">0</span>].a  <span class="comment">// 2</span></span><br></pre></td></tr></table></figure></p>
<hr>
<h3 id="7-reverse"><a href="#7-reverse" class="headerlink" title="7. reverse()"></a>7. reverse()</h3><p>用于颠倒排列数组元素，返回改变后的数组，<strong>会改变原数组</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</span><br><span class="line">arr.reverse();  <span class="comment">//  [5, 4, 3, 2, 1]</span></span><br><span class="line">arr  <span class="comment">//  [5, 4, 3, 2, 1]</span></span><br></pre></td></tr></table></figure></p>
<hr>
<h3 id="8-sort"><a href="#8-sort" class="headerlink" title="8. sort()"></a>8. sort()</h3><p>对数组成员进行排序，默认是按照字典顺序排序。排序后，<strong>会改变原数组</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">"Banana"</span>, <span class="string">"Orange"</span>, <span class="string">"Apple"</span>, <span class="string">"Mango"</span>];</span><br><span class="line">arr.sort(); <span class="comment">//  ["Apple", "Banana", "Mango", "Orange"]</span></span><br><span class="line">arr  <span class="comment">//   ["Apple", "Banana", "Mango", "Orange"]</span></span><br></pre></td></tr></table></figure></p>
<p>sort的参数函数本身接受两个参数，表示进行比较的两个数组成员。如果该<strong>函数的返回值大于0</strong>，表示<strong>第一个成员排在第二个成员后面</strong>；其他情况下，都是第一个元素排在第二个元素前面<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">40</span>,<span class="number">100</span>,<span class="number">1</span>,<span class="number">5</span>,<span class="number">25</span>,<span class="number">10</span>];</span><br><span class="line">arr.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;<span class="keyword">return</span> a-b&#125;);</span><br><span class="line"><span class="comment">//  [1, 5, 10, 25, 40, 100]</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">40</span>,<span class="number">100</span>,<span class="number">1</span>,<span class="number">5</span>,<span class="number">25</span>,<span class="number">10</span>];</span><br><span class="line">arr.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;<span class="keyword">return</span> b-a&#125;);</span><br><span class="line"><span class="comment">//  [100, 40, 25, 10, 5, 1]</span></span><br></pre></td></tr></table></figure>
<hr>
<h3 id="9-map"><a href="#9-map" class="headerlink" title="9. map()"></a>9. map()</h3><p>创建一个新数组，其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> num = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>];</span><br><span class="line">num.map(<span class="function"><span class="keyword">function</span>(<span class="params">n</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> n+<span class="number">1</span>;</span><br><span class="line">&#125;);  <span class="comment">//  [2, 3, 4, 5]</span></span><br><span class="line">num  <span class="comment">//  [1, 2, 3, 4]</span></span><br></pre></td></tr></table></figure></p>
<p>语法：array.map(function(currentValue,index,arr), thisValue)<br>| 参数 | 描述 |<br>| —– | —– |<br>| currentValue | 必须，当前元素的值 |<br>| index | 可选，当前元素的索引值 |<br>| arr | 可选，当前元素属于的数组对象 |<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>].map(<span class="function"><span class="keyword">function</span>(<span class="params">elem, index, arr</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> elem * index;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//  [0, 2, 6]</span></span><br></pre></td></tr></table></figure></p>
<p>elem为当前成员的值，index为当前成员的位置，arr为原数组（[1, 2, 3]）。<br>map方法还可以接受第二个参数，用来绑定回调函数内部的this变量。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>];</span><br><span class="line">[<span class="number">1</span>,<span class="number">2</span>].map(<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;<span class="keyword">return</span> <span class="keyword">this</span>[e]&#125;,arr);</span><br><span class="line"><span class="comment">//  ["b", "c"]  回调函数内部的this对象，指向arr数组</span></span><br></pre></td></tr></table></figure></p>
<hr>
<h3 id="10-forEach"><a href="#10-forEach" class="headerlink" title="10. forEach()"></a>10. forEach()</h3><p>用于调用数组的每个元素，并将元素传递给回调函数。<br>forEach方法和map类似，但是，不返回值，只用来操作数据。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">log</span>(<span class="params">e,i,arr</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'['</span>+i+<span class="string">']='</span> + e);</span><br><span class="line">&#125;</span><br><span class="line">[<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>].forEach(log);</span><br><span class="line"><span class="comment">// [0]=2</span></span><br><span class="line"><span class="comment">// [1]=3</span></span><br><span class="line"><span class="comment">// [2]=4</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> out = [];</span><br><span class="line">[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>].forEach(<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;<span class="keyword">this</span>.push(e*e)&#125;,out);</span><br><span class="line">out <span class="comment">//  [1,4,9]</span></span><br></pre></td></tr></table></figure>
<p>forEach，map方法不会跳过undefined和null，但会跳过空位。</p>
<hr>
<h3 id="11-filter"><a href="#11-filter" class="headerlink" title="11. filter()"></a>11. filter()</h3><p>用于过滤数组成员，创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>].filter(<span class="function"><span class="keyword">function</span> (<span class="params">elem</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (elem &gt; <span class="number">3</span>);</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//   [4, 5]</span></span><br></pre></td></tr></table></figure></p>
<p>语法：array.filter(function(currentValue,index,arr), thisValue)<br>filter方法的参数函数可以接受三个参数：当前成员，当前位置和整个数组。<br>filter方法还可以接受第二个参数，用来绑定参数函数内部的this变量。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123; <span class="attr">MAX</span>:<span class="number">3</span> &#125;;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">5</span>,<span class="number">6</span>];</span><br><span class="line">arr.filter(<span class="function"><span class="keyword">function</span>(<span class="params">item</span>)</span>&#123;</span><br><span class="line">   <span class="keyword">if</span>(item &gt; <span class="keyword">this</span>.MAX)</span><br><span class="line">   <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;,obj);  <span class="comment">//  [4, 5, 6]</span></span><br></pre></td></tr></table></figure></p>
<hr>
<h3 id="12-some-，every"><a href="#12-some-，every" class="headerlink" title="12. some()，every()"></a>12. some()，every()</h3><p>some() 方法用于检测数组中的元素是否满足指定条件（函数提供）。<br>every() 方法用于检测数组所有元素是否都符合指定条件（通过函数提供）。</p>
<p>some方法是<strong>只要一个成员</strong>的返回值是true，则整个some方法的返回值就是true，否则返回false。<br>every方法是<strong>所有成员</strong>的返回值都是true，整个every方法才返回true，否则返回false。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</span><br><span class="line">arr.some(<span class="function"><span class="keyword">function</span>(<span class="params">e,i,arr</span>)</span>&#123;<span class="keyword">return</span> e&gt;= <span class="number">3</span>&#125;);</span><br><span class="line"><span class="comment">// true</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</span><br><span class="line">arr.every(<span class="function"><span class="keyword">function</span>(<span class="params">e,i,arr</span>)</span>&#123;<span class="keyword">return</span> e &gt;= <span class="number">3</span>&#125;);</span><br><span class="line"><span class="comment">//  false</span></span><br></pre></td></tr></table></figure>
<hr>
<h3 id="13-reduce-，reduceRight"><a href="#13-reduce-，reduceRight" class="headerlink" title="13. reduce()，reduceRight()"></a>13. reduce()，reduceRight()</h3><p>reduce()：将数组元素计算为一个值（从左到右）<br>reduceRight()：将数组元素计算为一个值（从右到左）</p>
<p>array.reduce(function(total, currentValue, currentIndex, arr), initialValue)</p>
<blockquote>
<ul>
<li>total        : 必需，初始值，或者计算结束后的返回值</li>
<li>currentValue ：必需，当前元素</li>
<li>currentIndex ：可选，当前元素的索引</li>
<li>arr          ：可选，当前元素所属的数组对象</li>
<li>initialValue ：可选。传递给函数的初始值</li>
</ul>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>].reduce(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">   <span class="built_in">console</span>.log(a,b);</span><br><span class="line">   <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 1 2</span></span><br><span class="line"><span class="comment">// 3 3</span></span><br><span class="line"><span class="comment">// 6 4</span></span><br><span class="line"><span class="comment">// 10 5</span></span><br><span class="line"><span class="comment">// 15</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>].reduce(<span class="function"><span class="keyword">function</span> (<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;, <span class="number">10</span>);</span><br><span class="line"><span class="comment">//  25</span></span><br></pre></td></tr></table></figure>
<hr>
<h3 id="14-indexOf-，lastIndexOf"><a href="#14-indexOf-，lastIndexOf" class="headerlink" title="14. indexOf()，lastIndexOf()"></a>14. indexOf()，lastIndexOf()</h3><p>indexOf() 方法可返回某个指定的字符串值在字符串中<strong>首次</strong>出现的位置，没有出现则返回-1。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>];</span><br><span class="line">a.indexOf(<span class="string">'b'</span>) <span class="comment">//  1</span></span><br><span class="line">a.indexOf(<span class="string">'y'</span>) <span class="comment">//  -1</span></span><br></pre></td></tr></table></figure></p>
<p>语法：array.indexOf(item,start)</p>
<blockquote>
<ul>
<li>item  必须，查找的元素</li>
<li>start 可选的值，从0到stringObject.length-1 搜索的开始位置<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>].indexOf(<span class="string">'a'</span>, <span class="number">1</span>) <span class="comment">// -1</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</blockquote>
<p>lastIndexOf方法返回给定元素在数组中<strong>最后</strong>一次出现的位置，如果没有出现则返回-1。</p>
<hr>
<h2 id="三、参考链接"><a href="#三、参考链接" class="headerlink" title="三、参考链接"></a>三、参考链接</h2><p><a href="http://javascript.ruanyifeng.com/" target="_blank" rel="noopener">JavaScript 标准参考教程（alpha）</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  


          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/reimupage/images/avatar.gif"
                alt="Reimu" />
            
              <p class="site-author-name" itemprop="name">Reimu</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/reimupage/archives/">
              
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Reimu</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/reimupage/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/reimupage/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/reimupage/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/reimupage/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/reimupage/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/reimupage/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/reimupage/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/reimupage/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/reimupage/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/reimupage/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/reimupage/js/src/schemes/pisces.js?v=5.1.4"></script>



  

  


  <script type="text/javascript" src="/reimupage/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
